package com.common.spring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.model.supply.SupplyUserModel;
import com.model.sys.SysUser;

public class SpringMvcUtils {

	/**
	 * SpringMvc下获取request
	 *
	 * @return
	 */

	public static final HttpServletRequest getHttpServletRequest() {
		ServletRequestAttributes requestAttributes = ServletRequestAttributes.class
				.cast(RequestContextHolder.getRequestAttributes());
		if (requestAttributes == null) {
			return null;
		}
		HttpServletRequest contextRequest = requestAttributes.getRequest();
		return contextRequest;

	}

	public static final String getContext() {
		ServletRequestAttributes requestAttributes = ServletRequestAttributes.class
				.cast(RequestContextHolder.getRequestAttributes());
		if (requestAttributes == null) {
			return null;
		}
		HttpServletRequest contextRequest = requestAttributes.getRequest();
		return contextRequest.getContextPath();

	}

	/**
	 * 获取会话中用户类型
	 * 
	 * @return
	 */
	public static String getUserType() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return "";
		}
		HttpServletRequest request = attributes.getRequest();
		if (request.getSession().getAttribute("userType") == null) {
			return null;
		}
		String userTypeString = request.getSession().getAttribute("userType").toString();
		return userTypeString;
	}

	/**
	 * 获取当前登录用户
	 * 
	 * @return
	 */
	public static SysUser getSysUser() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return null;
		}
		HttpServletRequest request = attributes.getRequest();
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		return sysUser;
	}

	public static String getActive() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return null;
		}
		HttpServletRequest request = attributes.getRequest();
		String activeProfile = request.getSession().getServletContext().getInitParameter("spring.profiles.active");
		return activeProfile;
	}

	/**
	 * 获取供应商登录用户
	 * 
	 * @return
	 */
	public static SupplyUserModel getSupplyUser() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return null;
		}
		HttpServletRequest request = attributes.getRequest();
		SupplyUserModel sysUser = (SupplyUserModel) request.getSession().getAttribute("supplyUserModel");
		return sysUser;
	}

	public static HttpServletResponse getResponse() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return null;
		}
		HttpServletResponse response = attributes.getResponse();
		return response;
	}
}
